﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace _201206QuanLyBanHang.ChucNang
{
    public partial class DMChietKhau : Form
    {
        //khai bao bien
        private int makh;
        private string masp;
        private double giachietkhau;
        private DateTime ngaychietkhau;
        private string makv;

        //ben log chiet khau
        private double giasp;
        private int idlog;
        private int manhanvien;
        private string ghichu;

        //khai bao ket noi.

        bool flat = false;

        private int sodong = 0;

        private string sqlStr = "SELECT a.MAKH,a.MASP,c.tencuahang,b.tensp,GIACHIETKHAU,NGAYCHIETKHAU,ghichu_ck FROM CHIETKHAU a,sanpham b,khachhang c where a.makh=c.makh and a.masp=b.masp";
        private BLL_ChietKhau db = new BLL_ChietKhau();
        private DataSet ds = new DataSet();
        private DataSet ds1 = new DataSet();
        private DataSet ds2 = new DataSet();
        public int laystt()
        {
            int stt = 1;

            string sqlstring = "select max(STT_log2) from Log_ChietKhau";
            string err = "";
            object kq = db.layma(ref err, sqlstring);
            if (kq != System.DBNull.Value)
                stt = Convert.ToInt32(kq) + 1;
            return stt;
        }

        //hàm khoi tao cua lop
        public DMChietKhau()
        {
            InitializeComponent();

            DataBind(sqlStr);
            db.fillCombo("select makhuvuc,tenkhuvuc from khuvuc", this.cbkhuvuc, "tenkhuvuc", "makhuvuc");
            cbkhuvuc.SelectedIndex = -1;
            cbkhuvuc.Text = "chọn khu vực";
        }
        private void databindkhachhang(string sql)
        {
            ds1.Clear();
            ds1 = db.getDMkhachhang(sql);
            dgrkhachhang.DataSource = ds1.Tables[0];
            int n = dgrkhachhang.Rows.Count-1;
            //for (int i = 0; i <=n ; i++)
            //{
            //   this.dgrkhachhang.Rows[i].Cells["stt"].Value = i + 1;
            //}
            dgrkhachhang.Columns[0].HeaderText = "Mã KH";
            dgrkhachhang.Columns[0].Visible = false;
            dgrkhachhang.Columns[1].HeaderText = "Tên Cửa hàng";
        }
        //ham lay du lieu len luoi chieu khau
        public void DataBind(string sql)
        {
            ds.Clear();
            ds = db.getDMChietKhau(sql);

            this.dgchietkhau.DataSource = ds.Tables[0];
            this.dgchietkhau.Columns[0].HeaderText = "Mã KH";
            this.dgchietkhau.Columns[1].HeaderText = "Mã SP";

            this.dgchietkhau.Columns[0].Visible = false;
            this.dgchietkhau.Columns[1].Visible = false;

            this.dgchietkhau.Columns[2].HeaderText = "Tên CH";
            this.dgchietkhau.Columns[3].HeaderText = "Tên SP";

            this.dgchietkhau.Columns[4].HeaderText = "Giá chiết khấu";
            this.dgchietkhau.Columns[5].HeaderText = "Ngày chiết khấu";
            this.dgchietkhau.Columns[6].HeaderText = "Ghi chú";


            this.dgchietkhau.AutoResizeColumns(
              DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);

            this.dgchietkhau.ReadOnly = false;
            dgchietkhau.AllowUserToAddRows = false;
            sodong = 0;
            sodong = ds.Tables[0].Rows.Count;
            this.grchietkhau.Text = "Danh sách chiết khấu:";
            this.grchietkhau.Text = this.grchietkhau.Text + sodong;

        }
        //hàm lưu 
        private void luu()
        {
            try
            {
               // makh = int.Parse(this.cbkhachhang.SelectedValue.ToString());
                masp = this.cbsanpham.SelectedValue.ToString();
                giachietkhau = double.Parse(this.txtgiachietkhau.Text);
                ngaychietkhau = Convert.ToDateTime(this.datengaychietkhau.Value.ToShortDateString());
                ghichu = txtghichu.Text;


                string err = "";
                if (!db.insertDMChietKhau(ref err, makh, masp, giachietkhau, ngaychietkhau, ghichu))
                {
                    MessageBox.Show(err.ToString());
                }

                string sql1 = "SELECT a.MAKH,a.MASP,c.tencuahang,b.tensp,GIACHIETKHAU,NGAYCHIETKHAU,ghichu_ck FROM CHIETKHAU a,sanpham b,khachhang c ,khuvuc d where a.makh=c.makh and a.masp=b.masp and c.makhuvuc=d.makhuvuc and d.makhuvuc=" + makv;
                DataBind(sql1);
                string sqlkhachhang = "select a.tencuahang,c.tensp,c.giasp from khachhang a,chietkhau b,sanpham c where a.makh=b.makh and b.masp=c.masp and a.makh=" + makh;
                binkhachsp(sqlkhachhang);
                //DataBind(sqlStr);
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }


        }

        private void reset()
        {

            this.cbsanpham.SelectedIndex = -1;
            this.cbsanpham.Text = "---Chọn sản phẩm---";
            
            this.txtgiachietkhau.Focus();
            this.txtgiachietkhau.Text = "";
            this.txtghichu.Text = "";
            this.datengaychietkhau.Text = "";
        }

        private void BtnThem_Click(object sender, EventArgs e)
        {
            flat = false;
            cbkhuvuc.Enabled = true;
            cbsanpham.Enabled = true;
            db.fillCombo("select masp,tensp from sanpham", this.cbsanpham, "tensp", "masp");
            cbsanpham.SelectedIndex = -1;
            cbsanpham.Text = "---Chọn sản phẩm";

            this.BtnLuu.Enabled = true;
            this.BtnXoa.Enabled = true;
            enabled(true);
        }

        private void button_FormSanPham_Click(object sender, EventArgs e)
        {
            DmSanPham frsp = new DmSanPham();
            frsp.ShowDialog();
        }

        private void cbkhuvuc_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            if (cbkhuvuc.SelectedIndex!=-1)
            {
                makv = "";
                if (cbkhuvuc.SelectedValue.ToString()=="System.Data.DataRowView")
                {
                    makv = "null";
                    string sql = "select makh,tencuahang from khachhang a, khuvuc b where a.makhuvuc=b.makhuvuc and a.makhuvuc="+makv;
                    databindkhachhang(sql);
                    
                }
                else
                {
                    makv = cbkhuvuc.SelectedValue.ToString();
                    string sql = "select makh,tencuahang from khachhang a, khuvuc b where a.makhuvuc=b.makhuvuc and a.makhuvuc=" + makv;
                    databindkhachhang(sql);
                    string sql1 = "SELECT a.MAKH,a.MASP,c.tencuahang,b.tensp,GIACHIETKHAU,NGAYCHIETKHAU,ghichu_ck FROM CHIETKHAU a,sanpham b,khachhang c ,khuvuc d where a.makh=c.makh and a.masp=b.masp and c.makhuvuc=d.makhuvuc and d.makhuvuc=" + makv;
                    DataBind(sql1);
                }
                
            }
        }
        private void binkhachsp(string sqlkhsp)
        {
            ds2.Clear();
            ds2 = db.getDMChietKhau(sqlkhsp);

            this.dataGridView1.DataSource = ds2.Tables[0];
          
            dataGridView1.Columns[0].HeaderText = "Tên CH";
            dataGridView1.Columns[1].HeaderText = "Sản phẩm";
            dataGridView1.Columns[2].HeaderText = "Đơn giá";

        }
        private void dgrkhachhang_Click(object sender, EventArgs e)
        {
            makh = int.Parse(dgrkhachhang.CurrentRow.Cells["makh"].Value.ToString());
            //MessageBox.Show(makh.ToString());
            string sqlkhachhang ="select a.tencuahang,c.tensp,c.giasp from khachhang a,chietkhau b,sanpham c where a.makh=b.makh and b.masp=c.masp and a.makh="+makh;
            binkhachsp(sqlkhachhang);
        }

        private void BtnLuu_Click(object sender, EventArgs e)
        {
            if (this.txtgiachietkhau.Text != "")
            {
                if (flat)
                    DMSua();
                else
                    luu();
                //set doi tuong
                this.reset();
                enabled(false);
            }
            else
            {
                MessageBox.Show("giá không được để trống");
                this.txtgiachietkhau.Focus();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="t"></param>
        private void enabled(bool t)
        {

            this.cbsanpham.Enabled = t;
            this.txtgiachietkhau.Enabled = t;
            this.datengaychietkhau.Enabled = t;
            this.txtghichu.Enabled = t;
            
        }
        /// <summary>
        /// danh muc sua
        /// 
        /// </summary>
        private void DMSua()
        {
            string err = "";
            //makh = int.Parse(this.cbkhachhang.SelectedValue.ToString());

            masp = this.cbsanpham.SelectedValue.ToString();

            giachietkhau = double.Parse(this.txtgiachietkhau.Text);
            ngaychietkhau = Convert.ToDateTime(this.datengaychietkhau.Value.ToShortDateString());
            ghichu = this.txtghichu.Text;

            if (!db.updateDMChietKhau(ref err, makh, masp, giachietkhau, ngaychietkhau, ghichu))
            {
                MessageBox.Show(err.ToString());
            }
            else
            {
                //luulogchietkhau();
                string sql1 = "SELECT a.MAKH,a.MASP,c.tencuahang,b.tensp,GIACHIETKHAU,NGAYCHIETKHAU,ghichu_ck FROM CHIETKHAU a,sanpham b,khachhang c ,khuvuc d where a.makh=c.makh and a.masp=b.masp and c.makhuvuc=d.makhuvuc and d.makhuvuc=" + makv;
                DataBind(sql1);
            }
                //DataBind(sqlStr);
        }

        /// <summary>
        /// 
        /// </summary>
        private void DMXoa()
        {
            DialogResult result;
            result = MessageBox.Show("Bạn có muốn xóa không", "Xóa dữ liệu trong danh mục khu vực", MessageBoxButtons.YesNo);
            if (result == DialogResult.Yes)
            {
                string err = "Dữ liệu không thể xóa";
                if (!db.deleteDMChietKhau(ref err, makh, masp))
                    MessageBox.Show(err);
                else
                    MessageBox.Show("Đã xóa thành công");
            }
            string sql1 = "SELECT a.MAKH,a.MASP,c.tencuahang,b.tensp,GIACHIETKHAU,NGAYCHIETKHAU,ghichu_ck FROM CHIETKHAU a,sanpham b,khachhang c ,khuvuc d where a.makh=c.makh and a.masp=b.masp and c.makhuvuc=d.makhuvuc and d.makhuvuc=" + makv;
            DataBind(sql1);
            //DataBind(sqlStr);
        }
        private void BtnXoa_Click(object sender, EventArgs e)
        {
            DMXoa();
        }
        //sự kiện click vao luoi chiet khau
        private void dgchietkhau_Click(object sender, EventArgs e)
        {
            bin();
            enabled(true);

        }
        private void bin()
        {
            makh = int.Parse(dgchietkhau.CurrentRow.Cells["makh"].Value.ToString());
            masp = dgchietkhau.CurrentRow.Cells["masp"].Value.ToString();
            this.cbsanpham.SelectedValue = dgchietkhau[dgchietkhau.Columns[1].Index, dgchietkhau.CurrentRow.Index].Value.ToString();
            this.txtgiachietkhau.Text = dgchietkhau[dgchietkhau.Columns[4].Index, dgchietkhau.CurrentRow.Index].Value.ToString();
            this.datengaychietkhau.Text = dgchietkhau[dgchietkhau.Columns[5].Index, dgchietkhau.CurrentRow.Index].Value.ToString();
            this.txtghichu.Text = dgchietkhau[dgchietkhau.Columns[6].Index, dgchietkhau.CurrentRow.Index].Value.ToString();
            flat = true;
        }

        private void txttim_KeyUp(object sender, KeyEventArgs e)
        {
            this.dgrkhachhang.DataSource = db.LayThongtinTimkiem(this.cbkhuvuc.SelectedValue.ToString(), this.txttim.Text);
        }

        //


    }
}
